package io.renren.modules.job.task;

import io.renren.common.rpc.OutrestSimidaFeignRpc;
import io.renren.common.utils.DateUtils;
import io.renren.common.utils.RedisConst;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Set;

/**
 * @Auther: miaoguoxin
 * @Date: 2019/11/29 20:56
 * @Description: 延时匹配用户（N小时内）
 */
@Component("matchUserDelayTask")
@Slf4j
public class MatchUserDelayTask implements ITask {
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private OutrestSimidaFeignRpc outrestSimidaFeignRpc;

    @Override
    public void run(String params) {
        Date nowDate = new Date();
        Date date = DateUtils.addDateHours(nowDate, -4);
        //删除4小时前
        redisTemplate.opsForZSet().removeRangeByScore(RedisConst.USER_MATCH_DELAY_KEY, 0, date.getTime() - 2);
        Set<String> userIdSet = redisTemplate.opsForZSet().reverseRangeByScore(RedisConst.USER_MATCH_DELAY_KEY, 0, System.currentTimeMillis());
        if (userIdSet == null) {
            return;
        }
        for (String userIdstr : userIdSet) {
            long userId = NumberUtils.toLong(userIdstr);
            outrestSimidaFeignRpc.matchUserDelay(userId);
        }
    }
}
